package com.alipay.app.groupmeal.standard.ui.b.function;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.bean.TitleBean;
import com.alipay.app.groupmeal.standard.constant.BaseConstant;
import com.alipay.app.groupmeal.standard.constant.ConfigConstant;
import com.alipay.app.groupmeal.standard.listener.OnMyItemClickListener;
import com.alipay.app.groupmeal.standard.widget.business.BScreenRecyclerView;
import com.alipay.app.groupmeal.standard.widget.business.BScreenTitleView;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;
import com.alipay.app.groupmeal.standard.widget.business.BottomSnView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: 团餐
 * @ClassName: BScreenSettingList
 * @Description: B屏设置类型列表
 * @Author: bd-001935
 * @CreateDate: 4/14/22 4:08 PM
 * @UpdateUser: bd-001935
 * @UpdateDate: 4/14/22 4:08 PM
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class BScreenMenuList extends BaseBScreenView {

    private BScreenTitleView mBsTitleView;
    private BScreenRecyclerView mBsRecyclerView;
    private BottomSnView mBottomSnView;
    private List<TitleBean> mList = new ArrayList<>();
    private BaseQuickAdapter<TitleBean, BaseViewHolder> mAdapter;
    private View mFocusView;
    private View mFirstView;
    private boolean isFirst;

    public BScreenMenuList(Context context) {
        this(context, null);
    }

    public BScreenMenuList(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BScreenMenuList(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        isFirst = true;
    }

    @Override
    protected int getLayoutId() {
        if (ConfigConstant.displayType== BaseConstant.DISPLAY_TYPE.T2){
            return R.layout.view_list_ft2;
        }else {
            return R.layout.view_list;
        }
    }

    @Override
    protected void initView(View view) {
        mBsTitleView = view.findViewById(R.id.bsTitleView);
        mBsRecyclerView = view.findViewById(R.id.bsRecyclerView);
        mBottomSnView = view.findViewById(R.id.bsSnView);
    }

    @Override
    protected void initData() {
        mBsRecyclerView.setFocusable(false);
        initAdapter();
    }

    private void initAdapter() {
        mAdapter = new BaseQuickAdapter<TitleBean, BaseViewHolder>(R.layout.item_menu, mList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, TitleBean bean) {
                baseViewHolder.setText(R.id.tv_menu_title, bean.title);
                LinearLayout root = baseViewHolder.findView(R.id.ll_root);
                baseViewHolder.setText(R.id.tv_menu_num, baseViewHolder.getLayoutPosition() + 1 + "");
                setDefaultUI(baseViewHolder);
                root.setOnFocusChangeListener((v, hasFocus) -> {
                    if (hasFocus) {
                        mFocusView = root;
                        setFocusUI(baseViewHolder);
                    } else {
                        setDefaultUI(baseViewHolder);
                    }
                });
                if (isFirst && baseViewHolder.getLayoutPosition() == 0) {
                    mFocusView = root;
                    isFirst = false;
                    root.requestFocus();
                }
                if (baseViewHolder.getLayoutPosition() == 0) {
                    mFirstView = root;
                }
            }
        };
        mBsRecyclerView.setAdapter(mAdapter);
        mBsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false));
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (mOnMyItemClickListener != null)
                    mOnMyItemClickListener.onItemClick(adapter, view, position);
            }
        });
    }

    private void setFocusUI(BaseViewHolder baseViewHolder) {
        baseViewHolder.setTextColor(R.id.tv_menu_title, getContext().getColor(R.color.white));
        baseViewHolder.setTextColor(R.id.tv_menu_num, getContext().getColor(R.color.txt_1677FF));
        baseViewHolder.setBackgroundResource(R.id.tv_menu_num, R.drawable.shape_menu_num_bg_focus);
    }

    private void setDefaultUI(BaseViewHolder baseViewHolder) {
        baseViewHolder.setTextColor(R.id.tv_menu_title, getContext().getColor(R.color.txt_2A2A2A));
        baseViewHolder.setTextColor(R.id.tv_menu_num, getContext().getColor(R.color.txt_666666));
        baseViewHolder.setBackgroundResource(R.id.tv_menu_num, R.drawable.shape_menu_num_bg);
    }

    /**
     * 设置标题
     *
     * @param s
     */
    public void setTitleText(String s) {
        if (mBsTitleView != null)
            mBsTitleView.setTitle(s);
    }

    public void showTitle() {
        mBsTitleView.setVisibility(VISIBLE);
    }

    public void hideTitle() {
        mBsTitleView.setVisibility(GONE);
    }

    public void showSn() {
        mBottomSnView.setVisibility(VISIBLE);
    }

    public void hideSn() {
        mBottomSnView.setVisibility(GONE);
    }

    public void setList(List<TitleBean> mList) {
        this.mList = mList;
        mAdapter.setList(mList);
    }

    private OnMyItemClickListener mOnMyItemClickListener;

    public void setOnMyItemClickListener(OnMyItemClickListener onMyItemClickListener) {
        mOnMyItemClickListener = onMyItemClickListener;
    }

    public void request() {
        if (mFocusView != null)
            mFocusView.requestFocus();
        show();
    }

    /**
     * 关闭视图,下次重新开启时,需要重新初始化mFocusView
     */
    public void clearFocusView() {
        mFocusView = mFirstView;
        isFirst = true;
        request();
    }

}
